using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class PhysicCheck : MonoBehaviour
{
  private CapsuleCollider2D coll;
  [Header("检测参数")]
  public bool manual;
  public Vector2 bottomOffset;
  public Vector2 headOffset;
  public Vector2 backOffset;
  public float radius;
  public LayerMask platform;
  [Header("状态")]
  public bool isGround;
  public bool touchHeadWall;
  public bool touchBackWall;

  /// <summary>
  /// Awake is called when the script instance is being loaded.
  /// </summary>
  private void Awake()
  {
    coll = GetComponent<CapsuleCollider2D>();
    if (!manual)
    {
      headOffset = new Vector2(coll.offset.x - coll.bounds.size.x / 2, coll.bounds.size.y / 2);
      backOffset = new Vector2(coll.offset.x + coll.bounds.size.x / 2, headOffset.y);
    }
  }

  // Update is called once per frame
  void Update()
  {
    Check();
  }

  private void Check()
  {
    // 检测地面
    isGround = Physics2D.OverlapCircle((Vector2)transform.position + new Vector2(bottomOffset.x * transform.localScale.x, bottomOffset.y), radius, platform);
    // 检测墙体
    touchHeadWall = Physics2D.OverlapCircle((Vector2)transform.position + new Vector2(headOffset.x * transform.localScale.x, headOffset.y), radius, platform);
    touchBackWall = Physics2D.OverlapCircle((Vector2)transform.position + new Vector2(backOffset.x * transform.localScale.x, backOffset.y), radius, platform);
  }

  /// <summary>
  /// Callback to draw gizmos only if the object is selected.
  /// 在物体选择时，显示自定义Gizmos位置
  /// </summary>
  private void OnDrawGizmosSelected()
  {
    Gizmos.DrawWireSphere((Vector2)transform.position + new Vector2(bottomOffset.x * transform.localScale.x, bottomOffset.y), radius);
    Gizmos.color = Color.red;
    Gizmos.DrawWireSphere((Vector2)transform.position + new Vector2(headOffset.x * transform.localScale.x, headOffset.y), radius);
    Gizmos.color = Color.blue;
    Gizmos.DrawWireSphere((Vector2)transform.position + new Vector2(backOffset.x * transform.localScale.x, backOffset.y), radius);
  }
}
